import 'package:casdon/core/utils/string_externtion.dart';

class FeedbackDialogueModel {
  final String id;
  final String feedbackInfoId;
  final String? parentId;
  final String content;
  final List<String> imageUrls;
  final List<String> videoUrls;
  final String? videoCoverImage;
  final String createTime;
  final String? nickName;
  final String? sysUserNickName;
  final int? isPushAfterSale;

  bool get isServiceSide =>
      sysUserNickName != null && sysUserNickName!.isNotEmpty;
  bool get isSubject => parentId == null || parentId!.isEmpty;

  const FeedbackDialogueModel({
    required this.id,
    required this.feedbackInfoId,
    required this.parentId,
    required this.content,
    required this.imageUrls,
    required this.videoUrls,
    required this.videoCoverImage,
    required this.createTime,
    this.nickName,
    this.sysUserNickName,
    this.isPushAfterSale,
  });

  factory FeedbackDialogueModel.fromJson(Map<String, dynamic> json) {
    return FeedbackDialogueModel(
      id: json['id']?.toString() ?? '',
      feedbackInfoId: json['feedbackInfoId']?.toString() ?? '',
      parentId: json['parentId']?.toString() ?? '',
      content: json['content']?.toString() ?? '',
      imageUrls: _splitUrlsForImage(json['ossUrls']),
      videoUrls: _splitUrlsForVideo(json['ossUrls']),
      videoCoverImage: json['videoCoverImage'],
      createTime: json['createTime']?.toString() ?? '',
      nickName: json['nickName']?.toString(),
      sysUserNickName: json['sysUserNickName']?.toString(),
      isPushAfterSale: json['isPushAfterSale'] is int
          ? json['isPushAfterSale'] as int
          : int.tryParse(json['isPushAfterSale']?.toString() ?? ''),
    );
  }

  static List<String> _splitUrlsForImage(Object? raw) {
    if (raw == null) return const [];
    final value = raw.toString().trim();
    if (value.isEmpty) return const [];
    final urlList = value
        .split(',')
        .map((e) => e.trim())
        .where((e) => e.isNotEmpty)
        .toList();
    return urlList.where((e) => e.isImagePath()).toList();
  }

  static List<String> _splitUrlsForVideo(Object? raw) {
    if (raw == null) return const [];
    final value = raw.toString().trim();
    if (value.isEmpty) return const [];
    final urlList = value
        .split(',')
        .map((e) => e.trim())
        .where((e) => e.isNotEmpty)
        .toList();
    return urlList.where((e) => e.isVideoPath()).toList();
  }
}
